<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>编辑文件分类</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="/static/js/jquery.min.js?v=2.1.4"></script>
    <script src="/static/js/base.js"></script>
    <link rel="stylesheet" href="/static/layui/css/layui.css" media="all">
    <script src="/static/layui/layui.2.7.6.js"></script>
    <script src="/static/ueditor/ueditor.config.js"></script>
    <script src="/static/ueditor/ueditor.all.min.js"></script>
</head>
<body>

<div style="padding: 30px;background-color: #fff">
    <div class="layui-row layui-col-space15">
        <form class="layui-form" action="" lay-filter="formFilter" style="margin-top: 50px;">
            <div class="layui-form-item">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="name" required autocomplete="off" placeholder="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <input type="radio" name="status" checked value="1" lay-filter="status" title="正常">
                    <input type="radio" name="status" value="0" lay-filter="status" title="禁用">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <input type="hidden" name="id"  autocomplete="off" placeholder="" class="layui-input">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="lay-form">立即提交</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    var data=[];
    layui.use(['form', 'util', 'laydate','layer'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var util = layui.util;
        var laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });
        var id = getQueryVariable('id');
        if(id>0){
            $.ajax({
                url:'/admin/file/addCategory',
                data:{id:id},
                async:false,
                type:'get',
                dataType:'json',
                headers:{
                    'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
                    'accept':'*/*'
                },
                success:function(res){
                    if(res.code==1){
                        data = res.data[0]
                        form.val('formFilter', {
                            "id": data.id,
                            "name": data.name,
                            "status": data.status,
                        });
                    }else{
                        layer.alert(res.msg,{icon:5})//1√，2×，3问号，4锁，5不开心，6开心
                    }
                },fail:function(res){
                    layer.alert('请求失败',{icon:2})
                }
            })
        }

        //自定义验证规则
        form.verify({
            role_name: function(value){
                if(value.length < 2){
                    return '标题至少得2个字符啊';
                }
            }
        });
         //提交事件
        form.on('submit(lay-form)', function(data){
            // console.log(JSON.stringify(data.field));
            var load = layer.load(0, {shade: [0.3,'#fff']});
            $.ajax({
                url:'/admin/file/addCategory',
                data:data.field,
                async:false,
                type:'post',
                dataType:'json',
                headers:{
                    'token':localStorage.getItem('hypercmfname')+'_'+localStorage.getItem('token'),
                    'accept':'*/*'
                },
                success:function(res){
                    if(res.code==1){
                        layer.msg('操作成功',{
                            icon: 1,
                            time: 3000 //1秒关闭（如果不配置，默认是3秒）
                        },function () {
                            parent.layui.layer.closeAll()
                            parent.layui.table.reload('dataTable')
                        })

                    }else{
                        layer.alert(res.msg,{icon:5})//1√，2×，3问号，4锁，5不开心，6开心
                    }
                },fail:function(res){
                    layer.alert('请求失败',{icon:2})
                }
            })
        });


    });

    //layer弹出的子页面传值到当前页面
    function GetChildValue(data,from=''){
        if(from == 'video'){
            $("input[name=video]").attr("value",data[0].id)
        }else if(from == 'thumbnail'){
            console.log(from,data)

            $("input[name=thumbnail]").attr("value",data[0].url)
            layui.$('#uploadView').removeClass('layui-hide').find('img').attr('src', data[0].url);
        }
    }
</script>

</body>
</html>